package com.example.demo.restcontroller;


import com.example.demo.domain.Student;
import com.example.demo.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/webapi/student")
public class StudentRestController {

    @Autowired
    IStudentService studentService;

    @GetMapping("list")
    public List<Student> GetAll(){
        return studentService.getAll();
    }

    /**
     * 示例：http://localhost:8080/webapi/student/
     * 其中2表示id
     * @param id
     * @return
     */
    @GetMapping(value = "{id}")
    public Student GetById(@PathVariable Integer id){

        return studentService.findById(id);
    }

    @GetMapping(value = "delete/{id}")
    public boolean DeleteById(@PathVariable Integer id){
        studentService.delete(id);
        return true;
    }

    @PostMapping(value = "add")
    public Integer Add(@RequestBody Student student){
        studentService.save(student);
        return student.getId();
    }

    @PutMapping(value = "update")
    public Integer Update(@RequestBody Student student){
        studentService.save(student);
        return student.getId();
    }
}
